package com.example.graduation.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.graduation.entity.Goods;
import com.example.graduation.form.*;
import com.example.graduation.service.GoodsService;
import com.example.graduation.service.impl.GoodsServiceImpl;
import com.example.graduation.util.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xyc
 * @since 2022-02-11
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;


    @Resource
    private GoodsServiceImpl goodsServiceImpl;

    /**
     * @api {POST} /goods/findGoodsById 根据商品id和商店id查询商品信息
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName findGoodsById
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=8qHcm2vXw
     * @apiParam (请求体) {Number} goodsId
     * @apiParam (请求体) {Number} shopId
     * @apiParamExample 请求体示例
     * {"goodsId":9631,"shopId":9723}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/findGoodsById")
    @ResponseBody
    public R findGoodsById(@RequestBody FindGoodsForm form, @RequestHeader String token) {
        Goods goods = goodsService.findGoodsById(form.getGoodsId(), form.getShopId(), token);
        return R.ok("查询成功").put("goods", goods);
    }


    /**
     * @api {POST} /goods/findGoodsByType 根据商品类型查询商品
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName findGoodsByType
     * @apiParam (请求体) {String} type
     * @apiParam (请求体) {Number} page
     * @apiParam (请求体) {Number} size
     * @apiParam (请求体) {Number} status 这个得注意一下
     * @apiParamExample 请求体示例
     * {"size":6732,"page":7409,"type":"0p5tbG","status":4460}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/findGoodsByType")
    @ResponseBody
    public R findGoodsByType(@RequestBody FindGoodsByTypeForm form) {
        Page<Goods> page = goodsService.findGoodsByType(form.getType(), form.getPage(), form.getSize(), form.getStatus());
        return R.ok("查询成功").put("page", page);
    }


    /**
     * @api {POST} /goods/findGoodsByPriceLow 查询某个价格以下的商品
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName findGoodsByPriceLow
     * @apiParam (请求体) {String} type
     * @apiParam (请求体) {Number} price
     * @apiParam (请求体) {Number} page
     * @apiParam (请求体) {Number} size
     * @apiParam (请求体) {Number} status 这个得注意一下
     * @apiParamExample 请求体示例
     * {"size":3262,"price":5110.78,"page":1504,"type":"Mhn1","status":1860}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/findGoodsByPriceLow")
    @ResponseBody
    public R findGoodsByPriceLow(@RequestBody SelectGoodsByPriceForm form) {
        Page<Goods> page = goodsService.findGoodsByPriceLow(form.getType(), form.getPrice(), form.getPage(), form.getSize(), form.getStatus());
        return R.ok("查询成功").put("page", page);
    }


    /**
     * @api {POST} /goods/findGoodsByPriceBetween 查询某个价格区间的商品列表
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName findGoodsByPriceBetween
     * @apiParam (请求体) {String} type
     * @apiParam (请求体) {Number} lowPrice
     * @apiParam (请求体) {Number} highPrice
     * @apiParam (请求体) {Number} page
     * @apiParam (请求体) {Number} size
     * @apiParam (请求体) {Number} status 这个得注意一下
     * @apiParamExample 请求体示例
     * {"size":5287,"lowPrice":2507.83,"highPrice":6636.91,"page":9111,"type":"IzABJq8OaT","status":4456}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/findGoodsByPriceBetween")
    @ResponseBody
    public R findGoodsByPriceBetween(@RequestBody FindGoodsByPriceBetweenForm form) {
        Page<Goods> page = goodsService.findGoodsByPriceBetween(form.getType(), form.getLowPrice(), form.getHighPrice(), form.getPage(), form.getSize(), form.getStatus());
        return R.ok("查询成功").put("page", page);
    }


    /**
     * @api {POST} /goods/findGoodsBySale 根据价格排序搜索的商品
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName findGoodsBySale
     * @apiParam (请求体) {String} type
     * @apiParam (请求体) {Number} sale
     * @apiParam (请求体) {Number} page
     * @apiParam (请求体) {Number} size
     * @apiParam (请求体) {Number} status
     * @apiParam (请求体) {Number} status1
     * @apiParamExample 请求体示例
     * {"sale":457,"status1":1833,"size":2884,"page":1388,"type":"d6xsZ4","status":8795}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/findGoodsBySale")
    @ResponseBody
    public R findGoodsBySale(@RequestBody FindGoodsSaleForm form) {
        Page<Goods> page = goodsService.findGoodsBySale(form.getType(), form.getSale(), form.getPage(), form.getSize(), form.getStatus(), form.getStatus1());
        return R.ok("查询成功").put("page", page);
    }


    /**
     * @api {POST} /goods/applyMiaoSha 向管理员发送秒杀商品申请
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName applyMiaoSha
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=61FoXkZGv3
     * @apiParam (请求体) {Number} goodsId
     * @apiParamExample 请求体示例
     * {"goodsId":9602}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/applyMiaoSha")
    @ResponseBody
    public R applyMiaoSha(@RequestHeader String token, @RequestBody ApplyMiaoShaForm form) {
        goodsServiceImpl.applyMiaoSha(token, form.getGoodsId());
        return R.ok("发送成功");
    }


    /**
     * @api {POST} /goods/addGoods 添加商品
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName addGoods
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=6
     * @apiParam (请求体) {Number} goodsId
     * @apiParam (请求体) {Number} userId
     * @apiParam (请求体) {Number} shopId
     * @apiParam (请求体) {Number} count
     * @apiParam (请求体) {Number} oldPrice
     * @apiParam (请求体) {Number} newPrice
     * @apiParam (请求体) {String} type
     * @apiParam (请求体) {Number} status
     * @apiParam (请求体) {String} goodsName
     * @apiParam (请求体) {Number} createTime
     * @apiParam (请求体) {Number} sale
     * @apiParam (请求体) {Number} month
     * @apiParam (请求体) {String} photo
     * @apiParam (请求体) {String} describe
     * @apiParam (请求体) {String} notice
     * @apiParamExample 请求体示例
     * {"goodsId":6661,"oldPrice":89.12,"count":8351,"photo":"Ps","type":"tP","userId":163,"newPrice":2314.28,"sale":2812,"month":9250,"createTime":4483,"shopId":1614,"describe":"usg","goodsName":"JwPRJogv","status":9895,"notice":"LSgc"}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/addGoods")
    @ResponseBody
    public R addGoods(@RequestBody Goods goods, @RequestHeader String token) {
        goodsServiceImpl.addGoods(goods, token);
        return R.ok("添加商品成功");
    }


    /**
     * @api {POST} /goods/deleteGoods 根据商品id删除商品
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName deleteGoods
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=9hL
     * @apiParam (请求体) {Number} goodsId
     * @apiParamExample 请求体示例
     * {"goodsId":4936}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/deleteGoods")
    @ResponseBody
    public R deleteGoods(@RequestHeader String token, @RequestBody ApplyMiaoShaForm form) {
        goodsServiceImpl.deleteGoods(token, form.getGoodsId());
        return R.ok("删除商品成功");
    }


    /**
     * @api {POST} /goods/finggoodsByToken 查询用户商店的商品内容
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName finggoodsByToken
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=QgjnpPScq
     * @apiParam (请求体) {Number} page
     * @apiParam (请求体) {Number} size
     * @apiParamExample 请求体示例
     * {"size":4057,"page":1600}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/finggoodsByToken")
    @ResponseBody
    public R finggoodsByToken(@RequestHeader String token, @RequestBody PageSizeForm form) {
        Page<Goods> goodsPage = goodsServiceImpl.finggoodsByToken(token, form.getPage(), form.getSize());
        return R.ok().put("goodsPage", goodsPage);
    }


    /**
     * @api {POST} /goods/findallType 查找本用户的商品所有类型
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName findallType
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=jm2FB46X
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/findallType")
    @ResponseBody
    public R findallType(@RequestHeader String token) {
        Set<String> set = goodsServiceImpl.findallType(token);
        return R.ok().put("set", set);
    }


    /**
     * @api {POST} /goods/findallTypeById 根据店铺id查询店铺商品类型
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName findallTypeById
     * @apiParam (请求体) {Number} requestBody
     * @apiParamExample 请求体示例
     * 8522
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/findallTypeById")
    @ResponseBody
    public R findallTypeById(@RequestBody IdForm form) {
        Set<String> set = goodsServiceImpl.findallTypeById(form.getShopId());
        return R.ok().put("set", set);
    }


    /**
     * @api {POST} /goods/finggoodsByshopId 查找该商店的商品
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName finggoodsByshopId
     * @apiParam (请求体) {Number} shopId
     * @apiParam (请求体) {Number} page
     * @apiParam (请求体) {Number} size
     * @apiParamExample 请求体示例
     * {"size":9461,"shopId":3288,"page":1209}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/finggoodsByshopId")
    @ResponseBody
    public R finggoodsByshopId(@RequestBody IdAndPageSizeForm form) {
        Page<Goods> page = goodsServiceImpl.finggoodsByshopId(form.getShopId(), form.getPage(), form.getSize());
        return R.ok().put("page", page);
    }


    /**
     * @api {POST} /goods/likegoods 模糊查询
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName likegoods
     * @apiParam (请求体) {String} name
     * @apiParamExample 请求体示例
     * {"name":"k0LSYkRJN"}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/likegoods")
    @ResponseBody
    public R likegoods(@RequestBody NameForm form) {
        List<Goods> goods = goodsServiceImpl.likegoods(form.getName());
        return R.ok().put("goods", goods);
    }


    /**
     * @api {POST} /goods/changeGoods 修改商品信息
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName changeGoods
     * @apiParam (请求体) {Number} goodsId
     * @apiParam (请求体) {Number} userId
     * @apiParam (请求体) {Number} shopId
     * @apiParam (请求体) {Number} count
     * @apiParam (请求体) {Number} oldprice
     * @apiParam (请求体) {Number} newprice
     * @apiParam (请求体) {String} type
     * @apiParam (请求体) {Number} status
     * @apiParam (请求体) {String} goodsname
     * @apiParam (请求体) {Number} createTime
     * @apiParam (请求体) {Number} sale
     * @apiParam (请求体) {Number} month
     * @apiParam (请求体) {String} photo
     * @apiParam (请求体) {String} des
     * @apiParam (请求体) {String} notice
     * @apiParamExample 请求体示例
     * {"oldprice":6146.34,"goodsId":1869,"count":4822,"photo":"fRVndnDn","goodsname":"qy9uLHotB","type":"v6X5hx","userId":4293,"newprice":9846.97,"sale":1162,"des":"QpUPPo","month":7220,"createTime":7132,"shopId":8007,"status":5552,"notice":"I"}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/changeGoods")
    @ResponseBody
    public R changeGoods(@RequestBody Goods goods) {
        Goods goods1 = goodsServiceImpl.changeGoods(goods);
        return R.ok().put("goods", goods1);
    }


    /**
     * @api {POST} /goods/selectByid 查询商品byid
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName selectByid
     * @apiParam (请求体) {Number} shopId
     * @apiParamExample 请求体示例
     * {"shopId":8649}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/selectByid")
    @ResponseBody
    public R selectByid(@RequestBody IdForm form) {
        Goods goods = goodsServiceImpl.selectByid(form.getShopId());
        return R.ok().put("goods", goods);
    }


    /**
     * @api {POST} /goods/miaosha miaosha
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName miaosha
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=9ppbPTrJ
     * @apiParam (请求体) {String} name
     * @apiParam (请求体) {Number} count
     * @apiParam (请求体) {String} time
     * @apiParam (请求体) {String} content
     * @apiParamExample 请求体示例
     * {"name":"czRu5MY","count":5922,"time":"rxCI","content":"qeLVqD9mDN"}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/miaosha")
    @ResponseBody
    public R miaosha(@RequestHeader String token, @RequestBody MisoShaForm form) {
        goodsServiceImpl.miaosha(token, form.getName(), form.getCount(), form.getTime(), form.getContent());
        return R.ok();
    }


    /**
     * @api {POST} /goods/findallg findallg
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName findallg
     * @apiParam (请求体) {Number} shopId
     * @apiParamExample 请求体示例
     * {"shopId":9656}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/findallg")
    @ResponseBody
    public R findallg(@RequestBody IdForm form) {
        List<Goods> goods = goodsServiceImpl.findallg(form.getShopId());
        return R.ok().put("goods", goods);
    }


    /**
     * @api {POST} /goods/buyMiaoShaGoods 用户参加秒杀活动
     * @apiVersion 1.0.0
     * @apiGroup GoodsController
     * @apiName buyMiaoShaGoods
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * token=deKHO
     * @apiParam (请求体) {Number} shopId
     * @apiParamExample 请求体示例
     * {"shopId":3464}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/buyMiaoShaGoods")
    @ResponseBody
    public R buyMiaoShaGoods(@RequestHeader String token, @RequestBody IdForm form) {
        Boolean yes = goodsServiceImpl.buyMiaoShaGoods(token, form.getShopId());
        return R.ok("抢购成功");
    }


}

